import { defineStore } from "pinia";
import config from "@/config/index";
import { i18n } from "@/locales/setupI18n";
import { unref } from "vue";
import { APP_LOCALE_KEY } from "@/enums/cacheEnum";
import { createLocalStorage } from "@/utils/cache";

let ls = createLocalStorage();
/**
 * 国际化
 */
export const useLocaleStore = defineStore(`${config.localstoragePrefix}-i18n`, {
	state: () => ({
		locale: ls.getItem(APP_LOCALE_KEY) || "zh-cn"
	}),
	actions: {
		//设置 locale
		async setLocale(locale) {
			const currentLocale = unref(locale);
			if (currentLocale == this.locale) return locale;
			this.locale = locale;
			console.log(this.locale, "--f");
			i18n.global.locale = this.locale;
			console.log(i18n, "globalI18n.locale");
			ls.setItem(APP_LOCALE_KEY, this.locale);
		}
	},
	getters: {}
});
